﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace ERP.sys
{
    public partial class sysCreate : System.Web.UI.Page
    {
        #region[Define]
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnCreateHTML_Click(object sender, EventArgs e)
        {
            CreateForms();
        }
        #endregion

        #region[Function]
        protected void CreateForms()
        {
            try
            {
                string Path_Page_aspx = Server.MapPath(@"/Page/" + "" + txtFileName.Text + ".aspx");
                string Path_Page_cs = Server.MapPath(@"/Page/" + "" + txtFileName.Text + ".aspx.cs");
                
                #region[Page_aspx]
                StreamWriter _page_aspx;
                _page_aspx = File.CreateText(Path_Page_aspx);
                _page_aspx.WriteLine("<%@ Page Title=\"\"Language=\"C#\" MasterPageFile=\"~/Page/Site.Master\" EnableEventValidation=\"false\" AutoEventWireup=\"true\" CodeBehind=\"" + txtFileName.Text + ".aspx.cs\" Inherits=\"ERP.Page." + txtFileName.Text + "\" %>");
                _page_aspx.WriteLine("<asp:Content ID=\"Content1\" ContentPlaceHolderID=\"head\" runat=\"server\">");
                //-------Scripts------------------
                _page_aspx.WriteLine("<script>");
                _page_aspx.WriteLine("\tfunction pageLoad() {");
                _page_aspx.WriteLine("}");
                _page_aspx.WriteLine("</script>");
                //-------End------------------
                _page_aspx.WriteLine("</asp:Content>");
               
                _page_aspx.WriteLine("<asp:Content ID=\"Content2\" ContentPlaceHolderID=\"MainContent\" runat=\"server\">");
                //-------Create-ToolBar------------------
                _page_aspx.WriteLine("<asp:UpdatePanel ID=\"upDateAll\" runat=\"server\">");
                    _page_aspx.WriteLine("<ContentTemplate>");
                        _page_aspx.WriteLine("<!---------------Toolbar---------------->");
                            _page_aspx.WriteLine("<div class=\"toolbar-main\">");
                            if (ddlTypeButton.SelectedValue.ToString() == "0")//Linkbutton
                            {
                                _page_aspx.WriteLine("<asp:LinkButton ID=\"" + txtIDButton.Text + "Add\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Add" + "_Click\"><i class=\"fa fa-pencil-square-o\"></i> Thêm mới</asp:LinkButton>");
                                _page_aspx.WriteLine("<asp:LinkButton ID=\"" + txtIDButton.Text + "Delete\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClientClick=\"return confirmCheckIn(this)\" OnClick=\"" + txtIDButton.Text + "Delete" + "_Click\"><i class=\"fa fa-trash-o\"></i> Xoá</asp:LinkButton>");
                                if (chkFind.Checked)
                                {
                                    _page_aspx.WriteLine("<asp:TextBox ID=\"txtSearch\" CssClass=\"textbox-search\" runat=\"server\" />");
                                    _page_aspx.WriteLine("<asp:LinkButton ID=\"" + txtIDButton.Text + "Search\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Search" + "_Click\"><i class=\"fa fa-search\"></i> Tìm kiếm</asp:LinkButton>");
                                }
                                _page_aspx.WriteLine("<asp:LinkButton ID=\"" + txtIDButton.Text + "Reload\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Reload" + "_Click\"><i class=\"fa fa-refresh\"></i> Làm tươi</asp:LinkButton>");
                                _page_aspx.WriteLine("<asp:LinkButton ID=\"" + txtIDButton.Text + "Excel\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Excel" + "_Click\"><i class=\"fa fa-file-excel-o\"></i> Xuất file Excel</asp:LinkButton>");
                            }
                            if (ddlTypeButton.SelectedValue.ToString() == "1")//Button
                            {
                                _page_aspx.WriteLine("<asp:Button ID=\"" + txtIDButton.Text + "Add\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" Text=\"Thêm mới\" OnClick=\"" + txtIDButton.Text + "Add" + "_Click\" />");
                                _page_aspx.WriteLine("<asp:Button ID=\"" + txtIDButton.Text + "Delete\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClientClick=\"return confirmCheckIn(this)\" Text=\"Xoá\" OnClick=\"" + txtIDButton.Text + "Delete" + "_Click\" />");
                                if (chkFind.Checked)
                                {
                                    _page_aspx.WriteLine("<asp:TextBox ID=\"txtSearch\" CssClass=\"textbox-search\" runat=\"server\" />");
                                    _page_aspx.WriteLine("<asp:Button ID=\"" + txtIDButton.Text + "Search\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" Text=\"Tìm kiếm\" OnClick=\"" + txtIDButton.Text + "Search" + "_Click\" />");
                                }
                                _page_aspx.WriteLine("<asp:Button ID=\"" + txtIDButton.Text + "Reload\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" Text=\"Làm tươi\" OnClick=\"" + txtIDButton.Text + "Reload" + "_Click\" />");
                                _page_aspx.WriteLine("<asp:Button ID=\"" + txtIDButton.Text + "Excel\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" Text=\"Xuất file Excel\" OnClick=\"" + txtIDButton.Text + "Excel" + "_Click\" />");
                            }
                            if (ddlTypeButton.SelectedValue.ToString() == "2")//ImagesButton
                            {
                                _page_aspx.WriteLine("<asp:ImageButton ID=\"" + txtIDButton.Text + "Add\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Add" + "_Click\" />");
                                _page_aspx.WriteLine("<asp:ImageButton ID=\"" + txtIDButton.Text + "Delete\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClientClick=\"return confirmCheckIn(this)\" OnClick=\"" + txtIDButton.Text + "Delete" + "_Click\" />");
                                if (chkFind.Checked)
                                {
                                    _page_aspx.WriteLine("<asp:TextBox ID=\"txtSearch\" CssClass=\"textbox-search\" runat=\"server\" />");
                                    _page_aspx.WriteLine("<asp:ImageButton ID=\"" + txtIDButton.Text + "Search\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Search" + "_Click\" />");
                                }
                                _page_aspx.WriteLine("<asp:ImageButton ID=\"" + txtIDButton.Text + "Reload\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Reload" + "_Click\" />");
                                _page_aspx.WriteLine("<asp:ImageButton ID=\"" + txtIDButton.Text + "Excel\" CssClass=\"" + txtCSS.Text + "\" runat=\"server\" OnClick=\"" + txtIDButton.Text + "Excel" + "_Click\" />");
                            }
                            _page_aspx.WriteLine("</div>");
                        _page_aspx.WriteLine("<!---------------Toolbar---------------->");

                        _page_aspx.WriteLine("<%=StrAlert %>");
                    _page_aspx.WriteLine("</ContentTemplate>");
                    //---------<Triggers>----------------
                    _page_aspx.WriteLine("<Triggers>");
                        _page_aspx.WriteLine("<asp:AsyncPostBackTrigger ControlID=\"btnAdd\" EventName=\"Click\" />");
                        _page_aspx.WriteLine("<asp:AsyncPostBackTrigger ControlID=\"btnDelete\" EventName=\"Click\" />");
                        _page_aspx.WriteLine("<asp:AsyncPostBackTrigger ControlID=\"btnSearch\" EventName=\"Click\" />");
                        _page_aspx.WriteLine("<asp:AsyncPostBackTrigger ControlID=\"btnReload\" EventName=\"Click\" />");
                    _page_aspx.WriteLine("</Triggers>");
                    //---------<Triggers>----------------
                _page_aspx.WriteLine("</asp:UpdatePanel>");
                //------------End------------------------
                _page_aspx.WriteLine("</asp:Content>");
                _page_aspx.Flush();
                _page_aspx.Close();
                #endregion

                #region[Page_cs]
                StreamWriter _page_cs;
                _page_cs = File.CreateText(Path_Page_cs);
                _page_cs.WriteLine("using System;");
                _page_cs.WriteLine("using System.Collections.Generic;");
                _page_cs.WriteLine("using System.Linq;");
                _page_cs.WriteLine("using System.Web;");
                _page_cs.WriteLine("using System.Web.UI;");
                _page_cs.WriteLine("using System.Web.UI.WebControls;");
                _page_cs.WriteLine("using System.Data;");
                _page_cs.WriteLine("using BLL;");
                _page_cs.WriteLine("using HTML;");
                _page_cs.WriteLine("using System.Globalization;");
                _page_cs.WriteLine("using System.Text;");
                _page_cs.WriteLine("using System.Web.UI.HtmlControls;");
                _page_cs.WriteLine("namespace ERP.Page");
                _page_cs.WriteLine("{");
                    _page_cs.WriteLine("public partial class " + txtFileName.Text + " : System.Web.UI.Page");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("#region[Define]");
                    _page_cs.WriteLine("protected string StrAlert = string.Empty;");
                    _page_cs.WriteLine("#endregion");
                    //--------------------------
                    _page_cs.WriteLine("#region[Controller]");
                    _page_cs.WriteLine("protected void Page_Load(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");
                    //----------Add-----------------
                    _page_cs.WriteLine("protected void btnAdd_Click(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");

                    //----------Search-------------
                    _page_cs.WriteLine("protected void btnSearch_Click(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");


                    //----------Reload------------
                    _page_cs.WriteLine("protected void btnReload_Click(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");

                    //----------Delete------------
                    _page_cs.WriteLine("protected void btnDelete_Click(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");

                    //----------Excel------------
                    _page_cs.WriteLine("protected void btnExcel_Click(object sender, EventArgs e)");
                    _page_cs.WriteLine("{");
                    _page_cs.WriteLine("}");
                    //----------End-----------------
                    _page_cs.WriteLine("#endregion");
                    //----------------------------
                    _page_cs.WriteLine("#region[Function]");
                    _page_cs.WriteLine("#endregion");
                    //--------------------------
                    _page_cs.WriteLine("}");
                _page_cs.WriteLine("}");
                _page_cs.Flush();
                _page_cs.Close();
                #endregion
                lbMsg.Text = "Tạo tập tin thành công tập tin<br />";
            }
            catch (Exception ex)
            {
                lbMsg.Text = ex.ToString();
            }
        }
        #endregion
    }
}